// pages/record_3/record_3.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current: 0,
    tabArr: [],
    pageCountWrap: [],
    secArr: [],
    img_arr: [],
    info: [],
    currentPage: 1,
    pageCount: 0,
  },
  chooseCheck: function(e) {
    var that = this
    var index = e.currentTarget.dataset.index
    var indexs = e.currentTarget.dataset.indexs
    var secArr = that.data.secArr
    for (var i = 0; i < secArr[indexs].secArr1.length; i++) {
      if (secArr[indexs].secArr1[i].displaytype == 1) {
        secArr[indexs].secArr1[i].items[index].checked = !secArr[indexs].secArr1[i].items[index].checked
        console.log(secArr[indexs].secArr1[i])
        if (secArr[indexs].secArr1[i].items[index].checked) {
          secArr[indexs].secArr1[i].description += secArr[indexs].secArr1[i].items[index].title + ";"
        } else {
          secArr[indexs].secArr1[i].description = secArr[indexs].secArr1[i].description.replace(secArr[indexs].secArr1[i].items[index].title + ";", "")
        }
      }


    }
    console.log(secArr)
    that.setData({
      secArr: secArr
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    var str = options.str
    var arr = JSON.parse(str)
    var val = JSON.parse(options.val)
    console.log(val)
    var title = val.title
    var org_pic = val.pic
    var hzid = val.hzid
    var hospital = val.hospital
    var time = val.dates
    console.log(time)
    var bgc = wx.getStorageSync('color')
    var page = 10
    var currentPage = that.data.currentPage
    var pageCount = that.data.pageCount
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    wx.setNavigationBarTitle({
      title: arr[0].name,
    })
    // for (var i = 0; i < arr.length; i++) {
    var name = arr[0].projectArr;
    // for (var j = 0, len = name.length; j < len; j++) {
    var zlid = name[0].id
    console.log(name)
    app.util.request({
      'url': 'entry/wxapp/Getzlinfo',
      'cachetime': '0',
      data: {
        zlid: zlid,
        page: page
      },
      success: function(res) {
        console.log(res)
        var data = res.data.data
        var obj = {}
        var secArr1 = JSON.parse(JSON.stringify(res.data.data))
        obj.secArr1 = []
        pageCount = Math.ceil(secArr1.length / 10)
        for (var j = (currentPage - 1) * 10, len = currentPage * 10; j < len; j++) {
          if (secArr1[j]) {
            obj.secArr1.push(secArr1[j])
          }
        }
        // obj.secArr1 = secArr1
        obj.id = zlid
        var tabArr = that.data.tabArr
        obj.name = tabArr[0].projectArr[0].name
        for (var i = 0; i < data.length; i++) {
          console.log(data[i].displaytype)
          if (secArr1[i].displaytype == 2) {
            secArr1[i].picker = {}
            secArr1[i].picker.items = []
            secArr1[i].picker.displayorder = ""
            console.log(secArr1[i].items)
            for (var j = 0, len = secArr1[i].items.length; j < len; j++) {
              console.log(1)
              secArr1[i].picker.items[j] = secArr1[i].items[j].title
            }
          }
          if (secArr1[i].displaytype == 0) {
            secArr1[i].radio = {}
            secArr1[i].radio.items = []
            secArr1[i].radio.displayorder = ""
            console.log(secArr1[i].items)
            secArr1[i].radio.items = secArr1[i].items
          }
        }
        var secArr = that.data.secArr

        console.log(secArr)
        secArr.push(obj)
        console.log(secArr, data)
        app.globalData.secArr1 = secArr[that.data.pageCountWrap.length].secArr1
        that.setData({
          secArr: secArr,
          pageCountWrap: that.data.pageCountWrap.concat([that.data.pageCountWrap.length]),
          currentShow: that.data.pageCountWrap.length,
          currentErji: secArr[0].id,
          pageCount: pageCount,
          currentPage: currentPage + 1,
          data: res.data.data,
          currentEr: 0
        })
      },
    })
    // }
    // }

    var uniacid = app.siteInfo.uniacid;
    app.util.request({
      'url': 'entry/wxapp/url',
      'cachetime': '0',
      success: function(res) {
        console.log(res)
        that.setData({
          url: res.data
        })
      },
    })
    that.setData({
      tabArr: arr,
      time: time,
      hospital: hospital,
      hzid: hzid,
      org_pic: org_pic,
      title: title,
      uniacid: uniacid
    })
    var bgc = wx.getStorageSync('color')
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: bgc,
    })
    that.setData({
      backgroundColor: bgc,

    })
    var con = options.con
  },
  //切换Tab
  // switchTab: function(e) {
  //   var that = this
  //   this.setData({
  //     current: e.currentTarget.dataset.index
  //   })
  // },
  // secTabClick: function(e) {
  //   console.log(e)
  //   var that = this
  //   var flid = e.currentTarget.dataset.id
  //   var name = e.currentTarget.dataset.name
  //   console.log(flid, name)
  //   if (that.remove(that.data.pageCountWrap, e.currentTarget.dataset.index)) {
  //     app.util.request({
  //       'url': 'entry/wxapp/Zdybd',
  //       data: {
  //         id: flid
  //       },
  //       success: function(res) {
  //         console.log(res)
  //         var data = res.data.data
  //         var tabArr = that.data.tabArr
  //         var obj = {}
  //         var secArr1 = JSON.parse(JSON.stringify(res.data.data))
  //         obj.id = flid
  //         obj.name = name
  //         obj.secArr1 = secArr1
  //         for (var i = 0; i < data.length; i++) {
  //           console.log(data[i].displaytype)
  //           if (secArr1[i].displaytype == 2) {
  //             secArr1[i].picker = {}
  //             secArr1[i].picker.items = []
  //             secArr1[i].picker.displayorder = ""
  //             console.log(secArr1[i].items)
  //             for (var j = 0, len = secArr1[i].items.length; j < len; j++) {
  //               console.log(1)
  //               secArr1[i].picker.items[j] = secArr1[i].items[j].title
  //             }
  //           }
  //           if (secArr1[i].displaytype == 0) {
  //             secArr1[i].radio = {}
  //             secArr1[i].radio.items = []
  //             secArr1[i].radio.displayorder = ""
  //             console.log(secArr1[i].items)
  //             secArr1[i].radio.items = secArr1[i].items
  //           }
  //         }
  //         //console.log(e.currentTarget.dataset.index)
  //         console.log(res.data.data, secArr1)
  //         var secArr = that.data.secArr
  //         secArr.push(obj)
  //         console.log(secArr)
  //         that.setData({
  //           secArr: secArr,
  //           flid: flid,
  //           pageCountWrap: that.data.pageCountWrap.concat([e.currentTarget.dataset.index]),
  //           currentErji: e.currentTarget.dataset.index,
  //         })
  //       }
  //     })
  //   }

  //   that.setData({
  //     currentErji: e.currentTarget.dataset.index
  //   })
  // },
  //去重
  remove: function(arr, str) {
    for (var i = 0; i < arr.length; i++) {
      if (arr[i] == str) {
        return false
      }
    }
    return true
  },
  //单行文本
  lineClick: function(e) {
    console.log(e)
    var that = this
    var secArr = that.data.secArr
    var indexs = e.currentTarget.dataset.indexs
    var idx = e.currentTarget.dataset.idx
    secArr[indexs].secArr1[idx].description = e.detail.value
    that.setData({
      secArr: secArr
    })
  },
  //完成
  nextClick: function() {
    wx.navigateBack({
      delta: 4
    })
  },
  //上传图片
  choosePhoto: function(e) {
    var that = this
    var secArr = that.data.secArr
    console.log(secArr)
    var index = e.currentTarget.dataset.index
    var indexs = e.currentTarget.dataset.indexs
    var idx = e.currentTarget.dataset.idx
    wx.chooseImage({
      count: 9, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function(res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        secArr[indexs].secArr1[idx].items = secArr[indexs].secArr1[idx].items.concat(res.tempFilePaths)
        secArr[indexs].secArr1[idx].description = secArr[indexs].secArr1[idx].items
        if (secArr[indexs].secArr1[idx].items.length >= 9) {
          secArr[indexs].secArr1[idx].items.length = 9
        }
        console.log(secArr)
        that.setData({
          secArr: secArr
        })
      }
    })
  },
  // choosePhoto: function (e) {
  //   var that = this;
  //   console.log(this.data.img_arr)
  //   if (this.data.img_arr.length < 1) {
  //     wx.chooseImage({
  //       sizeType: ['original', 'compressed'],
  //       success: function (res) {
  //         that.setData({
  //           img_arr: that.data.img_arr.concat(res.tempFilePaths)
  //         })
  //       }
  //     })
  //   } else {
  //     wx.showToast({
  //       title: '最多上传一张图片',
  //       icon: 'loading',
  //       duration: 3000
  //     });
  //   }
  // }, 
  upload: function() {
    var that = this
    var uniacid = that.data.uniacid
    console.log(this.data.img_arr)
    for (var i = 0; i < this.data.img_arr.length; i++) {
      wx.uploadFile({
        url: that.data.url + 'app/index.php?i=' + uniacid + '&c=entry&a=wxapp&do=Upload&m=hyb_bm',
        filePath: that.data.img_arr[i],
        name: 'upfile',
        formData: adds,
        success: function(res) {
          console.log(res)
        }
      })
    }
    this.setData({
      formdata: ''
    })
  },
  //删除图片
  delClick: function(e) {
    var that = this
    var secArr = that.data.secArr
    var index = e.currentTarget.dataset.index
    var indexs = e.currentTarget.dataset.indexs
    var idx = e.currentTarget.dataset.idx
    secArr[indexs].secArr1[idx].items.splice(index, 1)
    that.setData({
      secArr: secArr
    })
  },
  //提交
  subClick: function(e) {
    // console.log(e)
    // console.log(this.data.secArr)
    var that = this
    var hzid = that.data.hzid
    var org_pic = that.data.org_pic
    var time = that.data.time
    var hospital = that.data.hospital
    var title = that.data.title
    var values = e.detail.value
    var secArr = this.data.secArr
    var current = that.data.current
    var tabArr = that.data.tabArr
    var currentErji = that.data.currentErji
    var currentEr = that.data.currentEr
    // console.log(tabArr[current])
    var bg_id = values.bg_id
    // console.log(bg_id)
    console.log(tabArr, current)
    var obj = {}
    obj.id = tabArr[current].id
    obj.name = tabArr[current].name
    console.log(currentErji, secArr)
    for (var i = 0, len = secArr.length; i < len; i++) {
      if (secArr[i].id == currentErji) {

        obj.data = secArr[i]
      }
    }
    console.log(secArr)
    // console.log(obj)
    for (var i = 0; i < obj.data.secArr1.length; i++) {
      delete obj.data.secArr1[i].uniacid
      delete obj.data.secArr1[i].activityid
      delete obj.data.secArr1[i].content
      delete obj.data.secArr1[i].displayorder
      delete obj.data.secArr1[i].essential
      delete obj.data.secArr1[i].fieldstype
      if (obj.data.secArr1[i].displaytype != 3) {
        delete obj.data.secArr1[i].highStandard
        delete obj.data.secArr1[i].lowStandard
      }
      if (obj.data.secArr1[i].displaytype != 0 && obj.data.secArr1[i].displaytype != 2 && obj.data.secArr1[i].displaytype != 1) {
        delete obj.data.secArr1[i].items
      }
    }
    console.log(obj)
    //传值
    app.util.request({
      'url': 'entry/wxapp/Getvalues',
      data: {
        hzid:hzid,
        org_pic: org_pic,
        time: time,
        hospital: hospital,
        title: title,
        bg_id: bg_id,
        values: obj,
        useropenid: wx.getStorageSync('openid')
      },
      success: function(res) {
        console.log(res)
        var bg_id = res.data.data
        that.setData({
          bg_id: bg_id
        })
      }
    })

    var data = that.data.data
    var currentPage = that.data.currentPage
    var pageCount = that.data.pageCount
    // console.log(currentPage, pageCount)
    if (that.pageRemove(that.data.pageCountWrap, that.data.currentShow+1)){
      console.log(1)
      if (currentPage > pageCount) {
        console.log(currentEr, tabArr[current].projectArr)
        if (currentEr < tabArr[current].projectArr.length - 1) {
          console.log(2)
          app.util.request({
            'url': 'entry/wxapp/Zdybd',
            data: {
              id: tabArr[current].projectArr[currentEr + 1].id
            },
            success: function (res) {
              console.log(res)
              var data = res.data.data
              var obj = {}
              var secArr1 = JSON.parse(JSON.stringify(res.data.data))
              obj.secArr1 = []
              var currentPage = 1
              pageCount = Math.ceil(secArr1.length / 10)
              for (var j = (currentPage - 1) * 10, len = currentPage * 10; j < len; j++) {
                if (secArr1[j]) {
                  obj.secArr1.push(secArr1[j])
                }
              }
              // obj.secArr1 = secArr1
              var tabArr = that.data.tabArr
              obj.id = tabArr[current].projectArr[currentEr + 1].id
              obj.name = tabArr[current].projectArr[currentEr + 1].name
              for (var i = 0; i < data.length; i++) {
                console.log(data[i].displaytype)
                if (secArr1[i].displaytype == 2) {
                  secArr1[i].picker = {}
                  secArr1[i].picker.items = []
                  secArr1[i].picker.displayorder = ""
                  console.log(secArr1[i].items)
                  for (var j = 0, len = secArr1[i].items.length; j < len; j++) {
                    console.log(1)
                    secArr1[i].picker.items[j] = secArr1[i].items[j].title
                  }
                }
                if (secArr1[i].displaytype == 0) {
                  secArr1[i].radio = {}
                  secArr1[i].radio.items = []
                  secArr1[i].radio.displayorder = ""
                  console.log(secArr1[i].items)
                  secArr1[i].radio.items = secArr1[i].items
                }
              }
              var secArr = that.data.secArr

              console.log(secArr, that.data.currentShow)
              secArr.push(obj)
              console.log(secArr, data)
              app.globalData.secArr1 = secArr[that.data.pageCountWrap.length].secArr1
              that.setData({
                secArr: secArr,
                pageCountWrap: that.data.pageCountWrap.concat([that.data.pageCountWrap.length]),
                currentShow: that.data.pageCountWrap.length,
                currentErji: tabArr[current].projectArr[currentEr + 1].id,
                pageCount: pageCount,
                currentPage: currentPage + 1,
                data: res.data.data,
                currentEr: currentEr + 1
              })
            }
          })

          console.log(tabArr[current].projectArr[currentEr + 1].id)

        } else {
          console.log("fdsjfsklfsklfsl klskj foiw rfoiwe sl ")
          var currentEr = 0
          console.log(tabArr[current + 1])
          if (tabArr[current + 1]){
            var indarr = tabArr[current + 1].id
            console.log(indarr, tabArr)
            app.util.request({
              'url': 'entry/wxapp/Getzlinfo',
              'cachetime': '0',
              data: {
                zlid: tabArr[current + 1].projectArr[currentEr].id
              },
              success: function (res) {
                console.log(res)
                var data = res.data.data
                var obj = {}
                var secArr1 = JSON.parse(JSON.stringify(res.data.data))
                obj.secArr1 = []
                var currentPage = 1
                pageCount = Math.ceil(secArr1.length / 10)
                for (var j = (currentPage - 1) * 10, len = currentPage * 10; j < len; j++) {
                  if (secArr1[j]) {
                    obj.secArr1.push(secArr1[j])
                  }
                }
                // obj.secArr1 = secArr1
                var tabArr = that.data.tabArr
                console.log(tabArr, current, currentEr)
                obj.id = tabArr[current+1].projectArr[currentEr].id
                obj.name = tabArr[current+1].projectArr[currentEr].name
                for (var i = 0; i < data.length; i++) {
                  console.log(data[i].displaytype)
                  if (secArr1[i].displaytype == 2) {
                    secArr1[i].picker = {}
                    secArr1[i].picker.items = []
                    secArr1[i].picker.displayorder = ""
                    console.log(secArr1[i].items)
                    for (var j = 0, len = secArr1[i].items.length; j < len; j++) {
                      console.log(1)
                      secArr1[i].picker.items[j] = secArr1[i].items[j].title
                    }
                  }
                  if (secArr1[i].displaytype == 0) {
                    secArr1[i].radio = {}
                    secArr1[i].radio.items = []
                    secArr1[i].radio.displayorder = ""
                    console.log(secArr1[i].items)
                    secArr1[i].radio.items = secArr1[i].items
                  }
                }
                var secArr = that.data.secArr

                console.log(secArr)
                secArr.push(obj)
                console.log(secArr, data)
                app.globalData.secArr1 = secArr[that.data.pageCountWrap.length].secArr1
                that.setData({
                  secArr: secArr,
                  pageCountWrap: that.data.pageCountWrap.concat([that.data.pageCountWrap.length]),
                  currentShow: that.data.pageCountWrap.length,
                  currentErji: tabArr[current + 1].projectArr[currentEr].id,
                  pageCount: pageCount,
                  currentPage: currentPage + 1,
                  data: res.data.data,
                  currentEr: currentEr,
                  current: current + 1,
                })
              }
            })
          }else{
            wx.showModal({
              content: '已全部填写完毕',
              success:function(res){
                if(res.confirm){
                   wx.navigateBack({
                      delta: 4
                    })
                }
              }
            })
          }
         
        }
      } else {
        var obj = {}
        var secArr1 = JSON.parse(JSON.stringify(data))
        obj.secArr1 = []
        for (var j = (currentPage - 1) * 10, len = currentPage * 10; j < len; j++) {
          if (secArr1[j]) {
            obj.secArr1.push(secArr1[j])
          }
        }
        // obj.secArr1 = secArr1

        var tabArr = that.data.tabArr
        obj.name = tabArr[0].projectArr[0].name
        for (var i = 0; i < data.length; i++) {
          // console.log(data[i].displaytype)
          if (secArr1[i].displaytype == 2) {
            secArr1[i].picker = {}
            secArr1[i].picker.items = []
            secArr1[i].picker.displayorder = ""
            // console.log(secArr1[i].items)
            for (var j = 0, len = secArr1[i].items.length; j < len; j++) {
              // console.log(1)
              secArr1[i].picker.items[j] = secArr1[i].items[j].title
            }
          }
          if (secArr1[i].displaytype == 0) {
            secArr1[i].radio = {}
            secArr1[i].radio.items = []
            secArr1[i].radio.displayorder = ""
            // console.log(secArr1[i].items)
            secArr1[i].radio.items = secArr1[i].items
          }
        }
        var secArr = that.data.secArr
        obj.id = that.data.currentErji
        // console.log(secArr)
        secArr.push(obj)
        // console.log(secArr, data)
        app.globalData.secArr1 = secArr[that.data.pageCountWrap.length].secArr1
        that.setData({
          secArr: secArr,
          pageCountWrap: that.data.pageCountWrap.concat([that.data.pageCountWrap.length]),
          currentShow: that.data.pageCountWrap.length,
          currentPage: currentPage + 1,
        })
      }
    }else{
      console.log(2)
      that.setData({
        currentShow: that.data.currentShow+1,
      })
    }
  

   
  },
  //页面去重
  pageRemove(arr,str){
    console.log(arr,str)
    for(var i = 0 ; i < arr.length ; i++){
      if(arr[i] == str){
        console.log(arr[i],str)
        return false
      }
    }
    return true
  },
  //上一步
  lastClick: function() {
    var that = this
    var tabArr = that.data.tabArr
    var current = that.data.current
    var currentEr = that.data.currentEr
    console.log(tabArr, currentEr)
    if (currentEr == 0){
      if (current > 0){
      current--
      }
      currentEr = tabArr[current].projectArr.length -1
      this.setData({
        currentEr: currentEr,
        current: current,
      })
    }
    console.log(tabArr)
    this.setData({
      currentShow: this.data.currentShow - 1
    })
  },
  //单选
  radioChange: function(e) {
    var that = this
    console.log(e)
    var secArr = that.data.secArr
    var idx = e.currentTarget.dataset.idx
    var indexs = e.currentTarget.dataset.indexs
    secArr[indexs].secArr1[idx].description = e.detail.value
    console.log(secArr)
    this.setData({
      secArr: secArr
    })
  },
  //picker
  bloodClick: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var that = this
    var secArr = that.data.secArr
    var idx = e.currentTarget.dataset.idx
    var indexs = e.currentTarget.dataset.indexs
    console.log(secArr[indexs])
    secArr[indexs].secArr1[idx].picker.displayorder = e.detail.value
    secArr[indexs].secArr1[idx].description = secArr[indexs].secArr1[idx].picker.items[e.detail.value]
    this.setData({
      secArr: secArr
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    //接收 多选
    console.log(2)
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})